//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package dao;

import java.sql.SQLException;
import java.util.List;
import model.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.DataSourceUtils;

public class UserDao {
    public UserDao() {
    }

    public void addUser(User user) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "insert into user(username,email,password,name,phone,address,isadmin,isvalidate) values(?,?,?,?,?,?,?,?)";
        r.update(sql, new Object[]{user.getUsername(), user.getEmail(), user.getPassword(), user.getName(), user.getPhone(), user.getAddress(), user.isIsadmin(), user.isIsvalidate()});
    }

    public boolean isUsernameExist(String username) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where username = ?";
        User u = (User)r.query(sql, new BeanHandler(User.class), new Object[]{username});
        return u != null;
    }

    public boolean isEmailExist(String email) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where email = ?";
        User u = (User)r.query(sql, new BeanHandler(User.class), new Object[]{email});
        return u != null;
    }

    public User selectByUsernamePassword(String username, String password) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where username=? and password=?";
        return (User)r.query(sql, new BeanHandler(User.class), new Object[]{username, password});
    }

    public User selectByEmailPassword(String email, String password) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where email=? and password=?";
        return (User)r.query(sql, new BeanHandler(User.class), new Object[]{email, password});
    }

    public User selectById(int id) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where id=?";
        return (User)r.query(sql, new BeanHandler(User.class), new Object[]{id});
    }

    public void updateUserAddress(User user) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "update user set name = ?,phone=?,address=? where id = ?";
        r.update(sql, new Object[]{user.getName(), user.getPhone(), user.getAddress(), user.getId()});
    }

    public void updatePwd(User user) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "update user set password = ? where id = ?";
        r.update(sql, new Object[]{user.getPassword(), user.getId()});
    }

    public int selectUserCount() throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select count(*) from user";
        return ((Long)r.query(sql, new ScalarHandler())).intValue();
    }

    public List selectUserList(int pageNo, int pageSize) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user limit ?,?";
        return (List)r.query(sql, new BeanListHandler(User.class), new Object[]{(pageNo - 1) * pageSize, pageSize});
    }

    public void delete(int id) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "delete from user where id = ?";
        r.update(sql, id);
    }
}
